Sending data to a local database with an ESP and Raspberry PI

1. Raspberry PI

Working with SSH:

  • I used a Raspberry Pi model 3 B with Rasbian lite installed on it.

  • I configured SSH


Making your IP-address static:

  • To make your IP-address static you need to write some lines of code into /etc/dhcpcd.conf

  • In the file add following lines:

Interface wlan0 // here you specify that you work with wifi

static_routers=[router ip] // routers IP address

static domain_name_server = [dns-ip] // your dns-server

static ip-address=[address you want]/24 //the ip you want to make static

  • to find out the routers ip and the dns-ip you can use this command:

$v sudo ip r

  • to find out the IP-address of the dns-server use this command:

$ grep “nameserver” /etc/resolv.conf

2. Setting up a database

Setting up a database:

  • I followed this tutorial LAMP web server

  • I ended with a wordpress server that was running on my raspberry pi connected to a database using mariaDB


Integrating the project to get temperature values from an ESP8266:

  • After the tutorial I created an extra folder in the /var/www/html called LOGGING

  • In this folder I created a php file to send data to my database → (temperature.php)


Sending information:

  • If you want to insert a value in the table you just need to type the url:

http://[ip-addres pi]/LOGGING/temperature.php?t=x


  • Where x represents the value that you want to insert.

<?php

$t = $_GET['t'];

$servername = "localhost";

$username = "root";

$password = "your password";

$dbname = "LOGGING";

// Create connection

$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection

if (!$conn) {

die("Connection failed: " . mysqli_connect_error());

}

$sql = "INSERT INTO temperature (temp)

VALUES ($t)";

if (mysqli_query($conn, $sql)) {

echo "New record created successfully";

} else {

echo "Error: " . $sql . "<br>" . mysqli_error($conn);

}

mysqli_close($conn);

?>

AARON FEYS

EURASMUS - 01

Sending data to a local database with an ESP and Raspberry PI

3. ESP8266

#include <ESP8266WiFi.h>

#include <ESP8266HTTPClient.h>

#include <WiFiClient.h>

const char* ssid = "HoeveLanghof";

const char* password = "vakantie";

//Your Domain name with URL path or IP address with path

String serverName = "http://192.168.1.143/LOGGING/temperature.php";

// the following variables are unsigned longs because the time, measured in

// milliseconds, will quickly become a bigger number than can be stored in an int.

unsigned long lastTime = 0;

unsigned long timerDelay = 5000;


void setup() {

Serial.begin(115200);

WiFi.begin(ssid, password);

Serial.println("Connecting");

while(WiFi.status() != WL_CONNECTED) {

delay(500);

Serial.print(".");

}

Serial.println("");

Serial.print("Connected to WiFi network with IP Address: ");

Serial.println(WiFi.localIP());

Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");

}

void loop() {

// Send an HTTP POST request depending on timerDelay

if ((millis() - lastTime) > timerDelay) {

int t = random(30); //radom generated temperature value

//Check WiFi connection status

if(WiFi.status()== WL_CONNECTED){

WiFiClient client;

HTTPClient http;String serverPath = serverName + "?t=" + t;

// Your Domain name with URL path or IP address with path

http.begin(client, serverPath.c_str());

// Send HTTP GET request

int httpResponseCode = http.GET();




AARON FEYS

EURASMUS - 02

Sending data to a local database with an ESP and Raspberry PI

if (httpResponseCode>0) {

Serial.print("HTTP Response code: ");

Serial.println(httpResponseCode);

String payload = http.getString();

Serial.println(payload);

}

else {

Serial.print("Error code: ");

Serial.println(httpResponseCode);

}

// Free resources

http.end();

}

else {

Serial.println("WiFi Disconnected");

}

lastTime = millis();

}

}


Code:

  • This code just sends random values to the server.

AARON FEYS

EURASMUS - 03